
*Set your directory to the location of the replication datasets
*cd "C:\Users\murdie\Dropbox\2022 Network Structure Stats Department Project\Replication datasets and script files to send to II"

ssc install sankey 
ssc install kountry

*Figure 5
clear all
import delimited "community_stage1.csv", clear
gen stage=1
save community1.dta, replace
clear all
import delimited "community_stage2.csv", clear
gen stage=2
save community2.dta, replace
import delimited "community_stage3.csv", clear
gen stage=3
save community3.dta, replace
import delimited "community_stage4.csv", clear
gen stage=4
save community4.dta, replace
append using "community1.dta" "community2.dta" "community3.dta"
sort stage countrycode

*generate a stage weighted centrality 
gen centrality2 = centrality/193.367 if stage==1
replace centrality2 = centrality/383435.1 if stage==2
replace centrality2=  centrality/18457.09 if stage==3
replace centrality2=  centrality/12407.9 if stage==4

gen centrality3 = centrality2 * 100

 
scatter community  centrality3  [w=centrality3], by(stage, rows(1)) msymbol(circle_hollow) ysc(reverse) 

*Figure 6
*cd "C:\Users\murdie\Dropbox\2022 Network Structure Stats Department Project\Replication datasets and script files to send to II"

import delimited "community_stage1.csv", clear
gen layer=0
tostring community, gen (source)
replace source="Community "+ source
rename community Community1
save community1.dta, replace
gen layer0 = layer
gen centality0 = centrality 
keep layer* source countrycode countrynames centrality*
save community1.dta, replace


import delimited "community_stage2.csv", clear
tostring community, gen (destination)
replace destination = "Community " + destination 
keep countrynames countrycode destination 
merge 1:1 countrynames using "community1.dta"
sankey centrality, from(source) to(destination) by(layer) sort1(name, reverse) sort2(order)
save layer0.dta, replace
keep if _merge==3
drop _merge 
save layer0.dta, replace


*Ok, conceivably this can work - now start with stage 2 as source and do it again 

import delimited "community_stage2.csv", clear
gen layer=1
tostring community, gen (source)
replace source="Community "+ source

save community2.dta, replace
gen layer1 = layer
gen centality1 = centrality 
keep layer* source countrycode countrynames centrality*
save community2.dta, replace


import delimited "community_stage3.csv", clear
tostring community, gen (destination)
replace destination = "Community " + destination 
keep countrynames countrycode destination 
merge 1:1 countrynames using "community2.dta"
save layer1.dta, replace
keep if _merge==3
drop _merge 
save layer1.dta, replace


*** now stage 3 as sorce and stage 4 as destination 

import delimited "community_stage3.csv", clear
gen layer=2
tostring community, gen (source)
replace source="Community "+ source

save community3.dta, replace
gen layer2 = layer
gen centality1 = centrality 
keep layer* source countrycode countrynames centrality*
save community3.dta, replace


import delimited "community_stage4.csv", clear
tostring community, gen (destination)
replace destination = "Community " + destination 
keep countrynames countrycode destination 
merge 1:1 countrynames using "community3.dta"
save layer2.dta, replace
keep if _merge==3
drop _merge 
save layer2.dta, replace


****for last stage, let's make stage 4 destination itself

import delimited "community_stage4.csv", clear
gen layer=3
tostring community, gen (source)
replace source="Community "+ source

save community4.dta, replace
gen layer3 = layer
gen centality1 = centrality 
keep layer* source countrycode countrynames centrality*
save community4.dta, replace


import delimited "community_stage4.csv", clear
tostring community, gen (destination)
replace destination = "Community " + destination 
keep countrynames countrycode destination 
merge 1:1 countrynames using "community4.dta"
save layer3.dta, replace
keep if _merge==3
drop _merge 
save layer3.dta, replace


use layer0.dta, replace
append using "layer1.dta" "layer2.dta" "layer3.dta"
sankey centrality, from(source) to(destination) by(layer) sort1(name, reverse) sort2(order)


gen centrality2 = centrality/193.367 if layer==0
replace centrality2 = centrality/383435.1 if layer==1
replace centrality2=  centrality/18457.09 if layer==2
replace centrality2=  centrality/12407.9 if layer==3

gen centrality3 = centrality2 * 100
sankey centrality3 if layer<3, from(source) to(destination) by(layer) sort1(name, reverse) sort2(order) noval 


sankey centrality3 if layer<3, from(source) to(destination) by(layer) sort1(name, reverse) sort2(order)  noval labprop labs(2) boxwid(5) 
